package cn.jbolt.core.db.util;

import com.alibaba.ttl.TransmittableThreadLocal;

/**
 * 当前线程的事务相关处理工具
 */
public class JBoltTxKit {
    //记录当前线程内有数据库事务处理 中间有错误状态的报错信息
    private static final TransmittableThreadLocal<String> txErrorMsg = new TransmittableThreadLocal<String>();
    //记录当前线程内有数据库事务处理 是否需要回滚
    private static final TransmittableThreadLocal<Boolean> txNeedRollback = new TransmittableThreadLocal<Boolean>();
    public static void setErrorMsg(String msg){
        txErrorMsg.set(msg);
    }

    public static String getErrorMsg(){
        return txErrorMsg.get();
    }

    public static void setNeedRollback(boolean needRollback){
        txNeedRollback.set(needRollback);
    }

    public static boolean isNeedRollback(){
        Boolean need = txNeedRollback.get();
        return need==null?false:need;
    }

    public static void clear(){
        if(isNeedRollback()){
            txNeedRollback.remove();
            txErrorMsg.remove();
        }
    }
}
